<?php
namespace app\service;
use think\facade\Db;
use app\model\SaleIncomeModel;
use Exception;

class SaleIncomeService{  
    public function list($params){
        $where=[];
        $page = $params['page'];
        $pageCount = $params['page_count'];

        //按出发日期查询
        if(!empty($params['start_date']) && !empty($params['end_date'])){
            if(!empty($params['start_date'])){
                $where[] = ['a.start_date','>= time',$params['start_date']];  
            }
            if(!empty($params['end_date'])){
                $where[] = ['a.start_date','<= time',$params['end_date']];  
            }
        }  

        if(!empty($params['typeid'])){
            $where[] = ['a.type','=',$params['typeid']];   
        }
        if(!empty($params['keyword'])){
            $where[] = ['a.content|a.payment_no|a.sn','like',"%{$params['keyword']}%"];   
        }
        if(!empty($params['payment_no'])){
            $where[] = ['a.payment_no','=',$params['payment_no']];  
        }
        if(!empty($params['sn'])){
            $where[] = ['a.sn','=',$params['sn']];  
        }
        if(!empty($params['client_id'])){
            $where[] = ['a.client_id','=',$params['client_id']];  
        }
        if(!empty($params['supplier_id'])){
            $where[] = ['a.supplier_id','=',$params['supplier_id']];  
        }
        if(!empty($params['business_type_id'])){
            $where[] = ['a.business_type_id','=',$params['business_type_id']];  
        }
        if(!empty($params['branch_id'])){
            $where[] = ['a.branch_id','=',$params['branch_id']];  
        }
        if(!empty($params['category_id'])){
            $where[] = ['a.category_id','=',$params['category_id']];  
        }
        if(!empty($params['operator_id'])){
            $where[] = ['a.operator_id','=',$params['operator_id']];  
        }

        
        $data = SaleIncomeModel::alias('a')
            ->field('a.*,b.title as branch,c.title as business_type,d.company_name as client,e.supplier_name as supplier,f.supplier_name as supplier2,g.cn_name as operator,h.name as category,i.cn_name as buyer')
            ->join('category b','a.branch_id=b.id','left')
            ->join('category c','a.business_type_id=c.id','left')
            ->join('client d','a.client_id=d.id','left')
            ->join('supplier e','a.supplier_id=e.id','left')
            ->join('supplier f','a.supplier_id2=f.id','left')
            ->join('user g','a.operator_id=g.id','left')
            ->join('product_type h','a.category_id=h.id','left')
            ->join('user i','a.buyer_id=i.id','left')
            ->where($where)->page($page,$pageCount)->order('id','desc')->select();
        foreach ($data as &$v) {            
            $v['receipts'] = Db::name('receipt')->where(['type'=>$v['type'],'income_id'=>$v['id']])->select(); 
        }
        $count = SaleIncomeModel::alias('a')->where($where)->count(); 
        return ["list"=>$data,"count"=>$count];
    } 
    
    public function info($id){
        $saleincomes = SaleIncomeModel::alias('a')
            ->field('a.*,b.company_name as client')       
            ->join('client b','a.client_id=b.id','left')  
            ->where('id',$id)->find(); 
        $saleincomes['received'] = Db::name('receipt')->where(['type'=>$saleincomes['type'],'income_id'=>$saleincomes['id']])->select(); 
        return $saleincomes;
    }

    public function infoByPid($type,$pid){
        $saleincomes = SaleIncomeModel::alias('a')   
            ->field('a.*,b.company_name as client')       
            ->join('client b','a.client_id=b.id','left')    
            ->where(['a.type'=>$type,'a.pid'=>$pid])->select(); 
        foreach ($saleincomes as &$v) {    
            $v['receipts'] = Db::name('receipt')->where(['type'=>$v['type'],'income_id'=>$v['id']])->select(); 
            foreach ($v['receipts'] as $r) {
                if($r['is_review']){
                    $v['receipt_review']=1;
                }
            }
        }
        return $saleincomes;
    }

    public function save($params){              
        if(empty($params['id'])){            
            $data = SaleIncomeModel::create($params); 
        }else{
            $data = SaleIncomeModel::update($params, ['id' => $params['id']]); 
        }        
        return $data;
    }

    public function delete($params){   
        Db::startTrans();
        try {       
            $income_data = Db::name('sale_income')->where('id',$params['id'])->find();
            $receipt_data = Db::name('receipt')->where(["income_id"=>$income_data["id"],"type"=>$income_data['type']])->select();
            foreach($receipt_data as $r){
                Db::name('receipt')->where(["id"=>$r["id"],"type"=>$r['type']])->delete();
            }
            SaleIncomeModel::destroy($params['id']);
            // 提交事务
            Db::commit();           
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
        }
    }

    public function csv($params){
        $where=[];        
        if(!empty($params['typeid'])){
            $where[] = ['type','=',$params['typeid']];   
        }
        if(!empty($params['keyword'])){
            $where[] = ['content','like',"%{$params['keyword']}%"];   
        }
        $data = SaleIncomeModel::alias('a')
            ->field('a.*,b.title as branch,c.title as business_type,d.company_name as client,e.supplier_name as supplier,f.supplier_name as supplier2,g.cn_name as operator,h.name as category,i.cn_name as buyer')
            ->join('category b','a.branch_id=b.id','left')
            ->join('category c','a.business_type_id=c.id','left')
            ->join('client d','a.client_id=d.id','left')
            ->join('supplier e','a.supplier_id=e.id','left')
            ->join('supplier f','a.supplier_id2=f.id','left')
            ->join('user g','a.operator_id=g.id','left')
            ->join('product_type h','a.category_id=h.id','left')
            ->join('user i','a.buyer_id=i.id','left')
            ->where($where)->order('id','desc')->select();
        foreach ($data as &$v) {            
            $v['receipts'] = Db::name('receipt')->where(['type'=>$v['type'],'income_id'=>$v['id']])->select(); 
        }
        return $data;
    } 
}